<?php

/**
 * The base controller which is used by the Front and the Admin controllers
 */
class MY_Model extends CI_Model {

	public $mytable = "";
    public $primaryKey = "";
    public $resCode = "";
	
	function __construct()
	{
		parent::__construct();
	}
	
	function field() {
		$fieldData = array();
		return $fieldData;
    }
    function get($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;

        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);

        $this->db->from($this->mytable);
        return $this->db->get()->first_row();
    }

    function gets($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);

        $this->db->from($this->mytable);
        return $this->db->get()->result();
    }

    function where_in($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;

        $this->db->from($this->mytable);
        if ($val != '')
            $this->db->where_in($this->mytable . "." . $param, $val);
        return $this->db->get()->result();
    }

    function num($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;

        $this->db->from($this->mytable);
        $this->db->where($param, $val);
        return $this->db->count_all_results();
    }

    function update($val = '', $param = null, $arr_update) {
        if ($param == null)
            $param = $this->primaryKey;

        if ($val != '')
            $this->db->where($param, $val);
        $arr_update = $this->msystem->update_security($arr_update, $this->field());
        $this->db->update($this->mytable, $arr_update);
    }

    function insert($arr_insert) {
        $arr_insert = $this->msystem->insert_security($arr_insert, $this->field());
        $this->db->insert($this->mytable, $arr_insert);
        return $this->db->insert_id();
    }

    function delete($val = '', $param = null) {
        if ($val != '') {
			if ($param == null)
				$param = $this->primaryKey;
			if (is_array($val) && $val != array())
				$this->db->where($val);
			else if ($val != '')
				$this->db->where($param, $val);
            $this->db->where($param, $val);
            $this->db->delete($this->mytable);
        }
    }
}
